package com.d3works.apps.quality.db.repo;

import com.d3works.apps.base.db.repo.SequenceRepository;
import com.d3works.apps.base.service.administration.SequenceService;
import com.d3works.apps.quality.db.QualityAlert;
import com.d3works.exception.D3worksException;
import com.d3works.exception.service.TraceBackService;
import com.google.common.base.Strings;
import com.google.inject.Inject;

public class QualityAlertManagementRepository extends QualityAlertRepository {

  @Inject private SequenceService sequenceService;

  /**
   * Generate and set sequence in reference with predefined prefix.
   *
   * @param qualityAlert Overridden quality alert object to set reference on onSave event.
   */
  @Override
  public QualityAlert save(QualityAlert qualityAlert) {
    if (Strings.isNullOrEmpty(qualityAlert.getReference())) {
      try {
        qualityAlert.setReference(
            sequenceService.getSequenceNumber(
                SequenceRepository.QUALITY_ALERT, null, QualityAlert.class, "reference"));
      } catch (D3worksException e) {
        TraceBackService.traceExceptionFromSaveMethod(e);
      }
    }
    return super.save(qualityAlert);
  }
}
